import * as fs from "fs";
import * as P from "path"
export class fileInfo {
    name: string;
    size: number;
}
/**
 * 递归查找一个文件夹下的所有文件
 * @param basepath 基础文件夹，得到的文件名会相对于他
 * @param path 
 * @returns 
 */
export function GetFilesSync(basepath: string, path: string, deeplimit: number = 3): fileInfo[] {
    let fileinfos: fileInfo[] = [];
    GetFilesSyncR(basepath, P.join(basepath, path), fileinfos, deeplimit);

    return fileinfos;
}

export function GetFilesSyncR(basepath: string, path: string, infos: fileInfo[], deeplimit: number): void {
    let files = fs.readdirSync(path, { encoding: "utf-8", withFileTypes: true });

    for (var i = 0; i < files.length; i++) {
        if (files[i].isFile()) {
            let fullname = P.join(path, files[i].name);
            let s = fs.statSync(fullname);
            let sname = fullname.substring(basepath.length + 1);
            infos.push({ size: s.size, name: sname });
        }

        if (deeplimit > 1 && files[i].isDirectory()) {

            let nextfilename = P.join(path, files[i].name);
            GetFilesSyncR(basepath, nextfilename, infos, deeplimit - 1);

        }
    }
}